# program that computes and draws Mandelbrot set, fixed point with 10 frac. bits

mandelbrot:
  # x y result iteration cx cy tmp
        1      40        $3 $3

  @@
    $2 ?'
      -- $:3
    ;
      ^ !@
    .

    $1 $0 * $1 $0 * - 1024 // $6 +  # tmp = (cx * cx - cy * cy) / 1024 + x
    $2 $2 * 512 // $6 + $:2         # cy = (cx * cy) / 512 + y
    $:2                             # cx = tmp
    
    # (cx * cx) // 1024 + (cy * cy) > 4194304, that's 4096 * 1024
    $1 $0 * $1 $0 * + 4194304 > ?
      0 $:4
      !@
    .
  .

  ^ ^ ^ $:2 ^
.

#------ main -------

-1023
@@ # for y

  -2047
  @@ # for x

    $0 $2 mandelbrot  # mandelbrot(x,y)
    " #" ?? ->        # print the point
    
    $0 32 +

    $0 512 >>= ?
      ^ !@
    ;
      $:1
    .
  .

  10 -> # newline

  $1 64 +
  $0 1024 >>= ?
    ^ !@
  ;
    $:1
  .
.
^
